package com.joelaws.prototype.client.service;

import com.google.gwt.user.client.ui.Image;
import com.gwtext.client.widgets.HTMLPanel;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.joelaws.prototype.client.data.NetworkingDetails;
import com.joelaws.prototype.client.data.User;

public class NetworkingTab extends Panel {

	private User user;

	private Panel body;

	public NetworkingTab(User user) {
		this.user = user;
		setLayout(new AnchorLayout());

		setAutoScroll(true);
		setBorder(false);
		setFrame(true);

		body = new Panel();

		add(getMainPanel());

		setTitle("Networking");
	}

	private Panel getMainPanel() {
		Panel panel = new Panel();

		final ToolbarButton profile = new ToolbarButton("Profile");
		final ToolbarButton browse = new ToolbarButton("Friends");
		final ToolbarButton search = new ToolbarButton("Groups");
		final ToolbarButton ratings = new ToolbarButton("Messages");
		final ToolbarButton networks = new ToolbarButton("Networks");
		final ToolbarButton events = new ToolbarButton("Events");

		Toolbar toolbar = new Toolbar();
		toolbar.addButton(profile);
		toolbar.addButton(browse);
		toolbar.addButton(search);
		toolbar.addButton(ratings);
		toolbar.addButton(networks);
		toolbar.addButton(events);

		body.add(getOtherPanel());

		panel.setTopToolbar(toolbar);
		panel.add(body);

		return panel;
	}

	private Panel getOtherPanel() {
		Panel body = new Panel();
		body.setLayout(new ColumnLayout());

		body.setAutoScroll(true);
		body.setBorder(false);
		body.setFrame(true);

		body.add(getLeft(), new ColumnLayoutData(.4));

		body.add(getRight(), new ColumnLayoutData(.6));

		return body;
	}

	private Panel getLeft() {
		Panel panel = new Panel();
		panel.setLayout(new AnchorLayout());
		panel.setHeight(800);

		String picLoc;
		if (user.getPicture() == null) {
			picLoc = "images/space_ghost.jpg";
		} else {
			picLoc = user.getPicture().getLocation();
		}

		Image image = new Image(picLoc);
		image.setSize("100px", "100px");
		panel.add(image);

		panel.add(new HTMLPanel("<br/><br/>"));

		Image ad = new Image("images/ad1.gif");
		ad.setSize("100px", "450px");
		panel.add(ad);

		return panel;
	}

	private Panel getRight() {
		Panel panel = new Panel();

		panel.setLayout(new AnchorLayout());

		panel
				.add(new HTMLPanel(user.getFirstName() + " "
						+ user.getLastName()));

		NetworkingDetails details = user.getNetworkingDetails();
		if (details != null) {
			panel.add(new HTMLPanel((details.isMale()) ? "Male" : "Female"));

			if (details.getBirthday() != null) {
				panel.add(new HTMLPanel("Birthday: "
						+ details.getBirthday().toString() + "<br/>"));
			}
			if (details.getDegree() != null) {
				panel.add(new HTMLPanel("Degree: "
						+ details.getDegree().toString() + "<br/>"));
			}
			if (details.getIncome() != null) {
				panel.add(new HTMLPanel("Income: "
						+ details.getIncome().toString() + "<br/>"));
			}
			if (details.getEmployment() != null) {
				panel.add(new HTMLPanel("Employment: "
						+ details.getEmployment().toString() + "<br/>"));
			}
			if (details.getReligion() != null) {
				panel.add(new HTMLPanel("Religion: "
						+ details.getReligion().toString() + "<br/>"));
			}

		}

		return panel;
	}

}
